{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "d1f20875-c50d-46df-906b-49ec1d0c6002",
   "metadata": {},
   "source": [
    "## 10.4 实现批标准化\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "c90a18b9-d8e8-431c-856c-35f1e4ed05e1",
   "metadata": {},
   "source": [
    "### 1.任务描述\n",
    "\n",
    "假设有一批样本（4个样本，每个样本5个特征），其特征矩阵为："
   ]
  },
  {
   "cell_type": "raw",
   "id": "fda35add-808b-436a-a224-ea33268deb25",
   "metadata": {},
   "source": [
    "[[2,1,0,2,3],\n",
    "[9,5,4,2,0],\n",
    "[2,3,4,5,6],\n",
    "[1,2,3,1,0]]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3903b806-5cb1-4796-b785-eb466876dec8",
   "metadata": {},
   "source": [
    "要求：\n",
    "\n",
    "- 计算样本每个特征的均值和方差\n",
    "- 对样本进行批标准化（均值为样本特征均值；方差为样本特征方差；样本偏移量为0、缩放比例为1；variance_epsilon为0.00001）"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "f5b4fc39-cbcf-432a-bf1e-e75e642d4b87",
   "metadata": {},
   "source": [
    "### 2.知识准备\n",
    "\n",
    "见教程。"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "55043130-4496-43a3-803b-9bc1cea8b1b8",
   "metadata": {},
   "source": [
    "### 3.任务分析构\n",
    "\n",
    "使用tf.nn.moments方法可以计算输入数组的均值和方差，使用axes=0指定按列（特征）来求解。计算出每项样本特征的均值和方差之后就可以使用tf.nn.batch_ normalization方法对样本数组进行批标准化了。\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "435c6090-cfda-4f46-a550-22a368e41e4a",
   "metadata": {},
   "source": [
    "### 4.任务实施\n"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "id": "ec75eb6c-5da3-467d-a471-ca3b47242dd6",
   "metadata": {},
   "source": [
    "执行代码"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "2ae9da58-e339-4d22-9f8d-ca255711d89e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "样本均值： tf.Tensor([3.5  2.75 2.75 2.5  2.25], shape=(5,), dtype=float32)\n",
      "样本方差： tf.Tensor([10.25    2.1875  2.6875  2.25    6.1875], shape=(5,), dtype=float32)\n",
      "输出\n",
      ": [[-0.46852106 -1.1832132  -1.6774812  -0.33333254  0.30151117]\n",
      " [ 1.7179106   1.521274    0.76249146 -0.33333254 -0.90453327]\n",
      " [-0.46852106  0.16903055  0.76249146  1.6666629   1.5075556 ]\n",
      " [-0.7808684  -0.5070914   0.15249825 -0.99999774 -0.90453327]]\n"
     ]
    }
   ],
   "source": [
    "import  tensorflow as tf\n",
    "import numpy as np\n",
    "# 1，定义输入\n",
    "input=tf.constant([[2,1,0,2,3],\n",
    "                     [9,5,4,2,0],\n",
    "                     [2,3,4,5,6],\n",
    "                     [1,2,3,1,0]],dtype=tf.float32)\n",
    "# 2，计算样本的均值和方差\n",
    "mean,variance = tf.nn.moments(input, axes = 0)\n",
    "print(\"样本均值：\",mean)\n",
    "print(\"样本方差：\",variance)\n",
    "# 3，批标准化\n",
    "y=tf.nn.batch_normalization(   \n",
    "    # 输入样本\n",
    "    x=input,   \n",
    "    # 样本均值\n",
    "    mean=mean,\n",
    "    # 样本方差\n",
    "    variance=variance,\n",
    "    # 样本偏移\n",
    "    offset=0,\n",
    "    # 缩放比例\n",
    "    scale=1,\n",
    "    # 为了避免分母为0，添加一个极小值\n",
    "    variance_epsilon=0.00001\n",
    ")\n",
    "print(\"输出\\n:\",y.numpy())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e6044c99-0741-4378-b2b6-f60c293cc3a9",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
